package
{
	import com.karst.essentials.chapter02.Chapter02View;
	import com.karst.essentials.chapter03.Chapter03View;
	import com.karst.essentials.chapter04.Chapter04View;
	import com.karst.essentials.chapter05.Chapter05View;
	import com.karst.essentials.chapter06.Chapter06View;
	import com.karst.essentials.chapter07.Chapter07View;
	import com.karst.essentials.chapter08.Chapter08View;
	import com.karst.essentials.chapter09.Chapter09View;
	import com.karst.essentials.chapter10.Chapter10View;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import org.aswing.AsWingManager;
	import org.aswing.CenterLayout;
	import org.aswing.Container;
	import org.aswing.EmptyLayout;
	import org.aswing.FlowLayout;
	import org.aswing.JButton;
	import org.aswing.JComboBox;
	import org.aswing.JWindow;
	
	[SWF(width="600", height="450", backgroundColor="#000000")]
	public class EssentialsStudy extends Sprite
	{
		public function EssentialsStudy()
		{
			initStage() ;
			createView() ;
		}

		private function initStage() : void
		{
			stage.frameRate = 40 ;
			AsWingManager.initAsStandard( this ) ;
		}

		private function createView() : void
		{
			createWindow() ;
			createComb() ;
			createViewBtn() ;
			addEvents() ;
		}

		private var window : JWindow = null ;
		private var pane : Container = null ;
		private function createWindow() : void
		{
			if( window == null )
			{
				window = new JWindow( this ) ;
				window.setSizeWH( stage.stageWidth, stage.stageHeight ) ;
			}
			
			if( pane == null )
			{
				pane = window.getContentPane() ;
				pane.setLayout( new EmptyLayout() ) ;
			}

			window.show() ;
		}

		private var chapterComb : JComboBox = null ;
		private var sectionComb : JComboBox = null ;
		private static const CHAPTER_LIST : Array = [ "第二章：构建你的第一个应用", "第三章：基本几何体",
			"第四章：Materials 材质", "第五章：Camera 摄影机", "第六章：移动物体", "第七章：着色",
			"第八章：外部模型", "第九章：深度排序", "第十章：粒子"] ;
		private function createComb() : void
		{
			if( chapterComb == null )
			{
				chapterComb = new JComboBox() ;
				chapterComb.setSizeWH( 220, 25 ) ;
				chapterComb.setLocationXY( 5, 5 ) ;
				chapterComb.setListData( CHAPTER_LIST ) ;
				chapterComb.setSelectedItem( CHAPTER_LIST[0] ) ;
			}
			if( sectionComb == null )
			{
				sectionComb = new JComboBox() ;
				sectionComb.setSizeWH( 235, 25 ) ;
				sectionComb.setLocationXY( 250, 5 ) ;
				sectionComb.setListData( Chapter02View.SECTION_LIST ) ;
				sectionComb.setSelectedItem( Chapter02View.SECTION_LIST[0] ) ;
			}
			pane.appendAll( chapterComb, sectionComb ) ;
		}

		private var viewBtn : JButton = null ;
		private function createViewBtn() : void
		{
			if( viewBtn == null )
			{
				viewBtn = new JButton( "View" ) ;
				viewBtn.setSizeWH( 60, 25 ) ;
				viewBtn.setLocationXY( 500, 5 ) ;
			}
			pane.append( viewBtn ) ;
		}


		
		//////////////////////////////////////////////////////////
		// chapter03-第一个应用
 		//////////////////////////////////////////////////////////
		private var chapter02 : Chapter02View = null ;
		private function createCh02View() : void
		{
			if( chapter02 == null )
				chapter02 = new Chapter02View() ;
			pane.addChild( chapter02 ) ;
			chapter02.showSection( sectionComb.getSelectedItem() ) ;
		}

		
		
		//////////////////////////////////////////////////////////
		// chapter03-几何体
 		//////////////////////////////////////////////////////////
		private var chapter03 : Chapter03View = null ;
		private function createCh03View() : void
		{
			if( chapter03 == null )
				chapter03 = new Chapter03View() ;
			pane.addChild( chapter03 ) ;
			chapter03.showSection( sectionComb.getSelectedItem() ) ;
		}

		
		
		//////////////////////////////////////////////////////////
		// chapter04-Materials材质
 		//////////////////////////////////////////////////////////
		private var chapter04 : Chapter04View = null ;
		private function createCh04View() : void
		{
			if( chapter04 == null )
				chapter04 = new Chapter04View() ;
			pane.addChild( chapter04 ) ;
			chapter04.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter05-Camera 摄影机
 		//////////////////////////////////////////////////////////
		private var chapter05 : Chapter05View = null ;
		private function createCh05View() : void
		{
			if( chapter05 == null )
				chapter05 = new Chapter05View() ;
			pane.addChild( chapter05 ) ;
			chapter05.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter056-Camera 摄影机
 		//////////////////////////////////////////////////////////
		private var chapter06 : Chapter06View = null ;
		private function createCh06View() : void
		{
			if( chapter06 == null )
				chapter06 = new Chapter06View() ;
			pane.addChild( chapter06 ) ;
			chapter06.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter07-Camera 着色
 		//////////////////////////////////////////////////////////
		private var chapter07 : Chapter07View = null ;
		private function createCh07View() : void
		{
			if( chapter07 == null )
				chapter07 = new Chapter07View() ;
			pane.addChild( chapter07 ) ;
			chapter07.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter08-外部模型
 		//////////////////////////////////////////////////////////
		private var chapter08 : Chapter08View = null ;
		private function createCh08View() : void
		{
			if( chapter08 == null )
				chapter08 = new Chapter08View() ;
			pane.addChild( chapter08 ) ;
			chapter08.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter09-深度排序
 		//////////////////////////////////////////////////////////
		private var chapter09 : Chapter09View = null ;
		private function createCh09View() : void
		{
			if( chapter09 == null )
				chapter09 = new Chapter09View() ;
			pane.addChild( chapter09 ) ;
			chapter09.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		//////////////////////////////////////////////////////////
		// chapter10-第十章：粒子
 		//////////////////////////////////////////////////////////
		private var chapter10 : Chapter10View = null ;
		private function createCh10View() : void
		{
			if( chapter10 == null )
				chapter10 = new Chapter10View() ;
			pane.addChild( chapter10 ) ;
			chapter10.showSection( sectionComb.getSelectedItem() ) ;
		}
		
		
		
		private function addEvents() : void
		{
			AsWingManager.getStage().addEventListener( Event.RESIZE, stageResizeHandler ) ;
			if( chapterComb )
				chapterComb.addActionListener( selectChapterHandler ) ;
			if( viewBtn )
				viewBtn.addEventListener( MouseEvent.CLICK, viewHandler ) ;
		}
		
		private function stageResizeHandler( event : Event ) : void
		{
			window.setSizeWH( stage.stageWidth, stage.stageHeight ) ;
		}
		
		private function selectChapterHandler( event : Event ) : void
		{
			if( sectionComb == null )
				return ;
			switch( chapterComb.getSelectedItem() )
			{
				case CHAPTER_LIST[0]:
					sectionComb.setListData( Chapter02View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[1]:
					sectionComb.setListData( Chapter03View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[2]:
					sectionComb.setListData( Chapter04View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[3]:
					sectionComb.setListData( Chapter05View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[4]:
					sectionComb.setListData( Chapter06View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[5]:
					sectionComb.setListData( Chapter07View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[6]:
					sectionComb.setListData( Chapter08View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[7]:
					sectionComb.setListData( Chapter09View.SECTION_LIST ) ;
					break;
				case CHAPTER_LIST[8]:
					sectionComb.setListData( Chapter10View.SECTION_LIST ) ;
					break;
				default:
					break;
			}
			sectionComb.setSelectedIndex( 0 ) ;
		}
		
		private  function viewHandler( event : MouseEvent = null ) : void
		{
			resetPaneView() ;
			switch( chapterComb.getSelectedItem() )
			{
				case CHAPTER_LIST[0]:
					createCh02View() ;
					break;
				case CHAPTER_LIST[1]:
					createCh03View() ;
					break;
				case CHAPTER_LIST[2]:
					createCh04View() ;
					break;
				case CHAPTER_LIST[3]:
					createCh05View() ;
					break;
				case CHAPTER_LIST[4]:
					createCh06View() ;
					break;
				case CHAPTER_LIST[5]:
					createCh07View() ;
					break;
				case CHAPTER_LIST[6]:
					createCh08View() ;
					break;
				case CHAPTER_LIST[7]:
					createCh09View() ;
					break;
				case CHAPTER_LIST[8]:
					createCh10View() ;
					break;
				default:
					break;
			}
		}

		private function resetPaneView() : void
		{
			pane.removeAll() ;
			while( pane.numChildren )
				pane.removeChildAt( 0 ) ;
			if( chapterComb )
				pane.appendAll( chapterComb, sectionComb, viewBtn ) ;
		}
	}
}